package com.example.utils;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
/**
 * 
 * @author lenovo
 * 方法介绍
 * 	n 		: 创建
 * 	a 		: 添加
 *  cv  	: 判断是否存在
 *  r  		: 删除
 *  c  		: 清理
 *  s  		: 大小
 *  g		: 取出
 *  to  	: 转化为原型
 *  each  	: 遍历
 *  filter  : 过滤
 *  group  	: 分组
 *  join	: 数组拼装为字符串，调用toString
 *  list	: 重写list的值，保持原有序列的顺序
 *  sort	: 排序
 * @param <T>
 */

public final class ListUtils<T> {

	@FunctionalInterface public static interface GroupIterable<V, K> { K accept(V v); }
	@FunctionalInterface public static interface EachIterable<T> { void accept(T t); }
	@FunctionalInterface public static interface ListNewList<T,N> { N accept(T t); }
	
	private List<T> list=new LinkedList<>();
	private ListUtils() {
	}
	public static <T> ListUtils<T> n(Collection<T> collection) {
		ListUtils<T> lu=new ListUtils<>();
		if(collection!=null)
			lu.list.addAll(collection);
		return lu;
	}
	public static <T> ListUtils<T> n() {
		return n(null);
	}
	public ListUtils<T> a(T t) {
		this.list.add(t);
		return this;
	}
	public ListUtils<T> a(@SuppressWarnings("unchecked") T ... ts) {
		for (T t : ts) {
			this.list.add(t);
		}
		return this;
	}
	public ListUtils<T> a(List<T> ts) {
		this.list.addAll(ts);
		return this;
	}
	public boolean cv(T t) {
		return this.list.contains(t);
	}
	public int s() {
		return this.list.size();
	}
	public T g(int index) {
		return this.list.get(index);
	}
	public ListUtils<T> r(int ... indexs) {
		for (int index : indexs) {
			this.list.remove(index);
		}
		return this;
	}
	public ListUtils<T> r( T t) {
		this.list.remove(t);
		return this;
	}
	public ListUtils<T> r(@SuppressWarnings("unchecked") T ... ts) {
		for (T t : ts) {
			this.list.remove(t);
		}
		return this;
	}
	public ListUtils<T> c() {
		this.list.clear();
		return this;
	}
	public List<T> to() {
		return this.list;
	}
	
	public ListUtils<T> each(EachIterable<? super T> eachIterable) {
		Iterator<T> it = this.list.iterator();
		while (it.hasNext()) {
			T t = (T) it.next();
			eachIterable.accept(t);
		}
		return this;
	}

	public ListUtils<T> sort(Comparator<? super T> comparator) {
		this.list.sort(comparator);
		return this;
	}
	
	public ListUtils<T> filter(Predicate<? super T> filter) {
		this.list.removeIf(filter);
		return this;
	}
	
	public <N> ListUtils<N> list(ListNewList<? super T,N> lnl) {
		ListUtils<N> lu=ListUtils.n();
		this.each(t->{
			N n=lnl.accept(t);
			lu.a(n);
		});
		return lu;
	}
	
	public <K> MapUtils<K,List<T>> group(GroupIterable<? super T, K> groupIterable) {
		MapUtils<K,List<T>> relust=MapUtils.n(); 
		this.each(t->{
			K key= groupIterable.accept(t);
			List<T> it=relust.ck(key)?relust.g(key):new LinkedList<>();
			it.add(t);
			relust.a(key, it);
		});
		return relust;
	}
	
	public String join(){
		return this.join(null);
	}
	
	public String join(String seg) {
		if(seg==null)
			seg="";
		String ret="";
		for (T t : list) {
			ret+=seg+t.toString();
		}
		return ret;
	}
}
